﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RouterControl.Whs.SingletonService;
using RouterControl.WhsAddIn.Wizards.SelectRouter;

namespace RouterControl.WhsAddIn.SettingsControls
{
    public partial class SelectedRouter : SettingsControlBase
    {
        public SelectedRouter(RouterControlSettingsTab2 tab)
			: base(tab)
		{
			if (_log.IsDebugEnabled)
				_log.Debug("Initialization start");

			try
			{
				InitializeComponent();
	
				this.lineBox.Header.Text = Properties.Resources.txtSettingsSelectedRouter;

				// done
				if (_log.IsDebugEnabled)
					_log.Debug("Initialization done");
			}
			catch (Exception ex)
			{
				_log.Fatal("Failed to initialize", ex);

				throw;
			}
		}

        private void selectRouterLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _log.Info("Running Select Router wizard...");

            try
            {
                var wiz = new WizardMain(_tab.Settings);

                if (wiz.Run() == DialogResult.OK)
                {
                    _tab.Commit();

                    _tab.RefreshCurrentState();
                }
            }
            catch (Exception x)
            {
                _log.Error("Failed launch Select Router wizard", x);

                Microsoft.HomeServer.Controls.QMessageBox.Show(
                    x.Message,
                    Properties.Resources.txtFailedLaunchSelectRouter,
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error
                    );
            }
        }

        private void rescanNetworkLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            _singletonService.Channel.SearchForDevices(false);

            _tab.SetState(RouterControlSettingsTab2.SettingsState.SearchingForRouter);
        }

        private void SelectedRouter_Load(object sender, EventArgs e)
        {
            if (_log.IsDebugEnabled)
                _log.Debug("Loading start");

            try
            {
                var id = _tab.Settings.SelectedRouterDeviceUniqueName;

                // router was found?
                var found = (
                    from r in _singletonService.Channel.GetFoundDevices()
                    where r.UniqueDeviceName == id
                    select (RootDeviceInfo?)r
                    ).FirstOrDefault();

                if (found != null)
                {
                    this.txtRouterNameLabel.Text = found.Value.FriendlyName;
                }
                else if (!string.IsNullOrEmpty(id))
                {
                    this.txtRouterNameLabel.Text = string.Format(
                        Properties.Resources.txtRouterNotFoundRouterName,
                        id
                        );
                }
                else
                {
                    this.txtRouterNameLabel.Text = Properties.Resources.txtSelectedRouterNone;
                }

                // done
                if (_log.IsDebugEnabled)
                    _log.Debug("Loading done");
            }
            catch (Exception ex)
            {
                _log.Fatal("Loading failed", ex);

                throw;
            }
        }
    }
}
